<?php
// 应用公共文件

/**
 * 邮件发送
 * @param        $to      接收人
 * @param string $subject 邮件标题
 * @param string $content 邮件内容(html模板渲染后的内容)
 * @throws Exception
 * @throws phpmailerException
 */
function send_email($to, $subject = '', $content = '')
{
    $mail   = new PHPMailer\PHPMailer\PHPMailer();
    // $arr    = \think\facade\Db::name('config')
    //     ->where('inc_type', 'smtp')
    //     ->select();
    // $config = convert_arr_kv($arr, 'name', 'value');

    $mail->CharSet = 'UTF-8'; //设定邮件编码，默认ISO-8859-1，如果发中文此项必须设置，否则乱码
    $mail->isSMTP();
    $mail->SMTPDebug = 0;
    //调试输出格式
    //$mail->Debugoutput = 'html';
    //smtp服务器
    $mail->Host = 'smtp.qq.com';
    //端口 - likely to be 25, 465 or 587
    $mail->Port = '465';

    if ($mail->Port == '465') {
        $mail->SMTPSecure = 'ssl';
    }// 使用安全协议
    //Whether to use SMTP authentication
    $mail->SMTPAuth = true;
    //发送邮箱
    $mail->Username = '1319683759@qq.com';
    //密码
    $mail->Password = 'qkaunwkmruvwfghf';
    //Set who the message is to be sent from
    $mail->setFrom('1319683759@qq.com', '安之助手');
    //回复地址
    //$mail->addReplyTo('replyto@example.com', 'First Last');
    //接收邮件方
    if (is_array($to)) {
        foreach ($to as $v) {
            $mail->addAddress($v);
        }
    } else {
        $mail->addAddress($to);
    }

    $mail->isHTML(true);// send as HTML
    //标题
    $mail->Subject = $subject;
    //HTML内容转换
    $mail->msgHTML($content);

    if(!$mail->send()){// 发送邮件
        echo "Message could not be sent.";
        echo "Mailer Error: ".$mail->ErrorInfo;// 输出错误信息
    }else{
        // echo '发送成功';
    }
}

//保存网络图片到本地
//$url 图片完整网络路径
function saveUrlImgLocal($url){
    $header = array(   
        'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',    
        'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',    
        'Accept-Encoding: gzip, deflate',);
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
    curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

    //下面两行代码用来绕过https协议
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

    $data = curl_exec($curl);
    $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    curl_close($curl);
    if ($code == 200) {
        //把URL格式的图片转成base64_encode格式的！    
        $imgBase64Code = "data:image/jpeg;base64," . base64_encode($data);
        $img_content=$imgBase64Code;//图片内容
        if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $img_content, $result)){ 
            $type = $result[2];//得到图片类型png?jpg?gif? 

            $nameTime = date("YmdHis").'_'.mt_rand();//文件名称随机数，以防文件重叠覆盖
            $new_file = './uploads/'.$nameTime.'.'.$type;

            if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $img_content)))){  
                return $nameTime.'.'.$type;;
            }
        }
    }
}

//检查目录是否存在，不存在则创建
function mkdirs($dir, $mode = 0777){
    if (is_dir($dir) || @mkdir($dir, $mode)) return TRUE;
    if (!mkdirs(dirname($dir), $mode)) return FALSE;
    return @mkdir($dir, $mode);
}
